home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Magazine / C_Tutorial / Part-4 / screen1.c < prev    next >
C/C++ Source or Header  |  1997-08-31  |  8KB  |  297 lines

  1. /* First, include our own supporting header files, i.e., clip.h */
  2. #include "clip.h"
  3.  
  4. #include<exec/libraries.h>
  5. #include<intuition/intuition.h>
  6. #include<utility/tagitem.h>
  7. #include<graphics/text.h>
  8. #include<graphics/rastport.h>
  9. #include<intuition/screens.h>
  10. #include<libraries/gadtools.h>
  11.  
  12. #include<string.h>
  13. #include<stdio.h>
  14.  
  15. #include<clib/exec_protos.h>
  16. #include<clib/graphics_protos.h>
  17. #include<clib/intuition_protos.h>
  18. #include<clib/gadtools_protos.h>
  19.  
  20. /* The library base global variables */
  21. /* (The different style of opening libraries requires these to be initialised to NULL) */
  22. struct Library* GfxBase = NULL;
  23. struct Library* IntuitionBase = NULL;
  24. struct Library* LayersBase = NULL;
  25. struct Library* GadToolsBase = NULL;
  26.  
  27. /* The global handle on the palette gadget */
  28. struct Gadget* palgad = NULL;
  29.  
  30. /* Need to give prototypes for our functions */
  31. void handleIDCMP(struct Window*);
  32. void setupWindow();
  33. void createWindow(struct Gadget*, struct Screen*);
  34. int openLibs();
  35. void closeLibs();
  36.  
  37. /* Some constants for the size of the window */
  38. #define MYWIN_WIDTH        (400)
  39. #define MYWIN_HEIGHT    (200)
  40.  
  41. /* Some constants for the position and size of our gadget */
  42. #define MYBUT_LEFT        (10)
  43. #define MYBUT_TOP            (5)
  44. #define MYBUT_WIDTH        (80)
  45. #define MYBUT_HEIGHT    (12)
  46. #define MYBUT_TEXT        "Next Pen"
  47. #define MYBUT_ID            (0)
  48.  
  49. #define MYPAL_LEFT        (170)
  50. #define MYPAL_TOP            (2)
  51. #define MYPAL_WIDTH        (109)
  52. #define MYPAL_HEIGHT    (19)
  53. #define MYPAL_TEXT        "Colour:"
  54. #define MYPAL_ID            (1)
  55. #define MYPAL_DEPTH        (4)
  56.  
  57. /* The top gap required around the gadgets */
  58. #define MYTOPGAP      (30)
  59.  
  60. /* The initial pen colour */
  61. #define MYINITPEN            (1)
  62.  
  63. /* The start of the program */
  64. void main()
  65. {
  66.     /* Use a different style of opening libraries... */
  67.     if(openLibs())
  68.     {
  69.         /* Now do the real work */
  70.         setupWindow();
  71.     }
  72.     /* Matched call to close libraries */
  73.     closeLibs();
  74. }
  75.  
  76. /* Try to open all the libraries -- return TRUE on success */
  77. int openLibs()
  78. {
  79.     if((GfxBase = OpenLibrary("graphics.library",37)) == NULL)
  80.     {
  81.         printf("Error: could not open graphics.library\n");
  82.         return FALSE;
  83.     }
  84.     if((IntuitionBase = OpenLibrary("intuition.library",37)) == NULL)
  85.     {
  86.         printf("Error: could not open intuition.library\n");
  87.         return FALSE;
  88.     }
  89.     if((LayersBase = OpenLibrary("layers.library",37)) == NULL)
  90.     {
  91.         printf("Error: could not open layers.library\n");
  92.         return FALSE;
  93.     }
  94.     if((GadToolsBase = OpenLibrary("gadtools.library",37)) == NULL)
  95.     {
  96.         printf("Error: could not open gadtools.library\n");
  97.         return FALSE;
  98.     }
  99.   return TRUE;
  100. }
  101.  
  102. /* Close any open library */
  103. void closeLibs()
  104. {
  105.     if(GadToolsBase)
  106.         CloseLibrary(GadToolsBase);
  107.     if(LayersBase)
  108.         CloseLibrary(LayersBase);
  109.     if(IntuitionBase)
  110.         CloseLibrary(IntuitionBase);
  111.     if(GfxBase)
  112.         CloseLibrary(GfxBase);
  113. }
  114.  
  115. /* Setup the window -- do the GadTools stuff */
  116. void setupWindow()
  117. {
  118.     struct Screen* scr;
  119.     /* Try to open a new screen with 16 colours (four bitplanes deep) */
  120.     if(scr = OpenScreenTags(NULL, SA_Depth, 4, TAG_DONE))
  121.     {
  122.         APTR vinfo;
  123.         /* Get the visual info so GadTools can render the gadgets nicely */
  124.         if(vinfo = GetVisualInfo(scr, TAG_DONE))
  125.         {
  126.             /* We can initialise glist in its declaration */
  127.             struct Gadget* glist = NULL;
  128.             struct Gadget* gad;
  129.             int offtop, offleft;
  130.             struct NewGadget newgad;
  131.             /* Initialised structure declaration: describes 8pt Topaz font */
  132.             struct TextAttr topazFont = { "topaz.font", 8, 0, 0, };
  133.             /* Start a GadTools gadget list */
  134.             gad = CreateContext(&glist);
  135.             /* The offsets of our window borders */
  136.             offleft = scr->WBorLeft;
  137.             offtop = scr->WBorTop + (scr->Font->ta_YSize + 1);
  138.  
  139.             /* Setup our first gadget */
  140.             newgad.ng_TextAttr         = &topazFont;
  141.             newgad.ng_VisualInfo     = vinfo;
  142.             newgad.ng_LeftEdge         = MYBUT_LEFT + offleft;
  143.             newgad.ng_TopEdge         = MYBUT_TOP + offtop;
  144.             newgad.ng_Width             = MYBUT_WIDTH;
  145.             newgad.ng_Height             = MYBUT_HEIGHT;
  146.             newgad.ng_GadgetText    = MYBUT_TEXT;
  147.             newgad.ng_GadgetID        = MYBUT_ID;
  148.             newgad.ng_Flags                = 0;
  149.             /* Now create it and add it to our list */
  150.             gad = CreateGadget(BUTTON_KIND, gad, &newgad, TAG_END);
  151.  
  152.             /* Setup our second gadget */
  153.             /* (We can reuse newgad, and just change the different bits) */
  154.             newgad.ng_LeftEdge         = MYPAL_LEFT + offleft;
  155.             newgad.ng_TopEdge         = MYPAL_TOP + offtop;
  156.             newgad.ng_Width             = MYPAL_WIDTH;
  157.             newgad.ng_Height             = MYPAL_HEIGHT;
  158.             newgad.ng_GadgetText    = MYPAL_TEXT;
  159.             newgad.ng_GadgetID        = MYPAL_ID;
  160.             newgad.ng_Flags                = 0;
  161.             /* Now create it and add it to our list */
  162.             if(gad = CreateGadget(PALETTE_KIND, gad, &newgad,
  163.                                                         /* Initially selected pen */
  164.                                                         GTPA_Color, MYINITPEN,
  165.                                                         /* Depth: 2 to the power MYPAL_DEPTH colours */
  166.                                                         GTPA_Depth, MYPAL_DEPTH,
  167.                                                         /* Gadget will indicate selection */
  168.                                                         GTPA_IndicatorWidth, 16,
  169.                                                         TAG_DONE))
  170.             {
  171.                 /* Remember gadget pointer so we can affect it in message handler */
  172.                 palgad = gad;
  173.                 /* If succeeded then all gadgets created */
  174.                 createWindow(glist, scr);
  175.             }
  176.             else
  177.                 printf("Error: could not create gadget(s)\n");
  178.             /* Free all the gadgets that were created */
  179.             FreeGadgets(glist);
  180.             FreeVisualInfo(vinfo);
  181.         }
  182.         else
  183.             printf("Error: could not get visual info\n");
  184.         CloseScreen(scr);
  185.     }
  186.     else
  187.         printf("Error: could not create screen\n");
  188. }
  189.  
  190. /* Actually open the window, in the normal way */
  191. void createWindow(struct Gadget* glist, struct Screen* scr)
  192. {
  193.     struct Window* win;
  194.     /* Open our window */
  195.     if(win = OpenWindowTags(NULL,
  196.                                                     WA_Width,                    MYWIN_WIDTH,
  197.                                                     WA_Height,                MYWIN_HEIGHT,
  198.                                                     WA_Flags,                    WFLG_CLOSEGADGET | WFLG_DRAGBAR | WFLG_REPORTMOUSE,
  199.                                                     WA_IDCMP,                    IDCMP_CLOSEWINDOW | IDCMP_MOUSEBUTTONS | IDCMP_MOUSEMOVE | BUTTONIDCMP | IDCMP_REFRESHWINDOW,
  200.                                                     WA_Gadgets,                glist,
  201.                                                     WA_CustomScreen,    scr,
  202.                                                     TAG_DONE,                    0))
  203.     {
  204.         /* If window opened, set clip region */
  205.         if(setClipInternal(win))
  206.         {
  207.             /* Let GadTools refresh its bits of the window */
  208.             GT_RefreshWindow(win, NULL);
  209.             /* Now handle messages */
  210.             handleIDCMP(win);
  211.             removeClip(win);
  212.         }
  213.         else
  214.             printf("Error: could not set clip region on window\n");
  215.         CloseWindow(win);
  216.     }
  217.     else
  218.         printf("Error: could not open window\n");
  219. }
  220.  
  221. /* Our message handling code */
  222. void handleIDCMP(struct Window* win)
  223. {
  224.     char* text = "Hello World!";
  225.     int going = TRUE;
  226.     int drawing = FALSE;
  227.     UBYTE pen = MYINITPEN;
  228.     SetAPen(win->RPort, pen);
  229.     /* Set the drawing mode to draw only the foreground of text, not the background */
  230.     SetDrMd(win->RPort, JAM1);
  231.     while(going)
  232.     {
  233.         struct IntuiMessage* intuimsg;
  234.         /* Wait for messages to arrive */
  235.         WaitPort(win->UserPort);
  236.         /* Messages have arrived: loop through all of them */
  237.         while(intuimsg = GT_GetIMsg(win->UserPort))
  238.         {
  239.             /* Act on this message... */
  240.             switch(intuimsg->Class)
  241.             {
  242.             case IDCMP_MOUSEBUTTONS:
  243.                 switch(intuimsg->Code)
  244.                 {
  245.                 case SELECTDOWN:
  246.                     drawing = TRUE;
  247.                     break;
  248.                 case SELECTUP:
  249.                     drawing = FALSE;
  250.                     break;
  251.                 }
  252.                 /* break; omitted so we draw on click, too */
  253.             case IDCMP_MOUSEMOVE:
  254.                 /* Don't draw on top gap which holds gadgets */
  255.                 if(drawing && intuimsg->MouseY > win->BorderTop+MYTOPGAP)
  256.                 {
  257.                     Move(win->RPort, intuimsg->MouseX, intuimsg->MouseY);
  258.                     Text(win->RPort, text, strlen(text));
  259.                 }
  260.                 break;
  261.             case IDCMP_CLOSEWINDOW:
  262.                 going = FALSE;
  263.                 break;
  264.             case IDCMP_REFRESHWINDOW:
  265.                 /* You *MUST* remember to ask for and handle these refresh messages */
  266.                 GT_BeginRefresh(win);
  267.                 GT_EndRefresh(win, TRUE);
  268.                 break;
  269.             case IDCMP_GADGETUP:
  270.                 /* Trick: introduce new "{..}" scope so we can declare gad locally */
  271.                 {
  272.                     struct Gadget* gad = (struct Gadget*)(intuimsg->IAddress);
  273.                     switch(gad->GadgetID)
  274.                     {
  275.                     case MYBUT_ID:
  276.                         /* Our button was clicked!  Set foreground to next pen colour */
  277.                         /* (Wrap when reached the end of the palette gadget's colours) */
  278.                         pen = (pen+1) % (1<<MYPAL_DEPTH);
  279.                         SetAPen(win->RPort, pen);
  280.                         /* Update palette gadget with new pen value */
  281.                         GT_SetGadgetAttrs(palgad, win, NULL, GTPA_Color, pen, TAG_DONE);
  282.                         break;
  283.                     case MYPAL_ID:
  284.                         /* Our palette gadget was clicked!  Set foreground to gadget colour */
  285.                         pen = intuimsg->Code;
  286.                         SetAPen(win->RPort, pen);
  287.                         break;
  288.                     }
  289.                     break;
  290.                 }
  291.             }
  292.             /* Reply when finished with message */
  293.             GT_ReplyIMsg(intuimsg);
  294.         }
  295.     }
  296. }
  297.